package com.finger.hospservice.controller.api;

import com.finger.commonutil.exception.YyghException;
import com.finger.hospservice.service.DepartmentService;
import com.finger.hospservice.service.HospSetService;
import com.finger.hospservice.service.HospitalService;
import com.finger.hospservice.service.ScheduleService;
import com.finger.modelservice.model.hosp.Department;
import com.finger.modelservice.model.hosp.Hospital;
import com.finger.modelservice.model.hosp.Schedule;
import com.finger.modelservice.result.Result;
import com.finger.modelservice.result.ResultCodeEnum;
import com.finger.serviceutil.helper.HttpRequestHelper;
import com.finger.serviceutil.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/api/hosp")
@CrossOrigin
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospSetService hospSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    // 移除排班
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest req){

        Map<String, Object> paramMap = HttpRequestHelper.switchMap(req.getParameterMap());

        String sign = (String) paramMap.get("sign");
        String hoscode = (String) paramMap.get("hoscode");

        String hospSignByHoscode = hospSetService.getHospSignByHoscode(hoscode);

        if(!Objects.equals(sign,MD5.encrypt(hospSignByHoscode))){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        String hosScheduleId = (String) paramMap.get("hosScheduleId");

        scheduleService.removeSchedule(hoscode,hosScheduleId);

        return Result.ok();
    }

    // 分页查询排班记录
    @PostMapping("schedule/list")
    public Result pageSchedule(HttpServletRequest req){

        Map<String, Object> paramMap = HttpRequestHelper.switchMap(req.getParameterMap());

        String sign = (String) paramMap.get("sign");
        String hoscode = (String) paramMap.get("hoscode");

        String hospSignByHoscode = hospSetService.getHospSignByHoscode(hoscode);

        if(!Objects.equals(sign,MD5.encrypt(hospSignByHoscode))){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        Integer page = StringUtils.isEmpty((String) paramMap.get("page"))?1:Integer.parseInt((String) paramMap.get("page"));
        Integer size = StringUtils.isEmpty((String) paramMap.get("limit"))?10:Integer.parseInt((String) paramMap.get("limit"));

//        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();

//        scheduleQueryVo.setHoscode((String) paramMap.get("hoscode"));
        Page<Schedule> result = scheduleService.pageDepartment(page,size,hoscode);

        return Result.ok(result);
    }

    // 上传排班接口
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest req){

        Map<String, Object> paramMap = HttpRequestHelper.switchMap(req.getParameterMap());

        String signRemote = (String) paramMap.get("sign");
        String hoscode = (String) paramMap.get("hoscode");
        String sign = hospSetService.getHospSignByHoscode(hoscode);

        // 签名校验
        if(!Objects.equals(signRemote,MD5.encrypt(sign))){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        scheduleService.save(paramMap);
        return Result.ok();
    }
    // 删除科室
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest req){

        Map<String, Object> paramMap = HttpRequestHelper.switchMap(req.getParameterMap());

        String sign = (String) paramMap.get("sign");
        String hoscode = (String) paramMap.get("hoscode");

        String hospSignByHoscode = hospSetService.getHospSignByHoscode(hoscode);

        if(!Objects.equals(sign,MD5.encrypt(hospSignByHoscode))){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        String depcode = (String) paramMap.get("depcode");

        departmentService.removeDepartment(hoscode,depcode);

        return Result.ok();
    }

    // 查询科室分页
    @PostMapping("department/list")
    public Result pageDepartment(HttpServletRequest req){

        Map<String, Object> paramMap = HttpRequestHelper.switchMap(req.getParameterMap());

        String sign = (String) paramMap.get("sign");
        String hoscode = (String) paramMap.get("hoscode");

        String hospSignByHoscode = hospSetService.getHospSignByHoscode(hoscode);

        if(!Objects.equals(sign,MD5.encrypt(hospSignByHoscode))){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        Integer page = StringUtils.isEmpty((String) paramMap.get("page"))?1:Integer.parseInt((String) paramMap.get("page"));
        Integer size = StringUtils.isEmpty((String) paramMap.get("limit"))?10:Integer.parseInt((String) paramMap.get("limit"));

        Page<Department> result= departmentService.pageDepartment(page,size,hoscode);

        return Result.ok(result);
    }

    // 保存科室
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest req){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(req.getParameterMap());

        String sign = (String) paramMap.get("sign");
        String hoscode = (String) paramMap.get("hoscode");

        String hospSignByHoscode = hospSetService.getHospSignByHoscode(hoscode);

        if(!Objects.equals(sign,MD5.encrypt(hospSignByHoscode))){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        departmentService.save(paramMap);

        return Result.ok();
    }

    // 上传医院
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest req){

        Map<String, Object> map = HttpRequestHelper.switchMap(req.getParameterMap());

        String hospSign = (String) map.get("sign");

        String hoscode = (String) map.get("hoscode");

        // 如果传过来的签名错误
        if(StringUtils.isEmpty(hoscode)){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }

        String hospSetSign = hospSetService.getHospSignByHoscode(hoscode);

        // MD5加密校验医院签名
        String signKey = MD5.encrypt(hospSetSign);

        if(!Objects.equals(hospSign,signKey)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        // 传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoDataString = (String)map.get("logoData");
        if(!StringUtils.isEmpty(logoDataString)) {
            String logoData = logoDataString.replaceAll(" ", "+");
            map.put("logoData", logoData);
        }

        hospitalService.saveHospital(map);

        return Result.ok();
    }

    // 查找医院
    @PostMapping("/hospital/show")
    public Result getHospital(HttpServletRequest req){

        Map<String, Object> paramMap = HttpRequestHelper.switchMap(req.getParameterMap());

        String hoscode = (String) paramMap.get("hoscode");

        Hospital hospital = hospitalService.getHospitalByHoscode(hoscode);

        if(hospital==null){
            throw new YyghException(ResultCodeEnum.DATA_ERROR);
        }

        String sign = (String) paramMap.get("sign");

        String hospSign = hospSetService.getHospSignByHoscode(hoscode);

        if(!Objects.equals(MD5.encrypt(hospSign),sign)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        return Result.ok(hospital);

    }



}
